package com.alipay.sofa.award.biz.service.impl;

import com.alipay.sofa.award.biz.service.facade.UserInfoFacade;
import com.alipay.sofa.award.common.dal.dao.UserInfoMapper;
import com.alipay.sofa.award.common.dal.dataobject.UserInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.support.TransactionCallback;
import org.springframework.transaction.support.TransactionTemplate;

/**
 * UserInfoImpl
 *
 * @author yangguanchao
 * @since 2018/09/05
 */
public class UserInfoImpl implements UserInfoFacade {

    private static Logger       logger = LoggerFactory.getLogger(UserInfoImpl.class);

    @Autowired
    private TransactionTemplate transactionTemplate;

    @Autowired
    private UserInfoMapper      userInfoMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.deleteByPrimaryKey(id);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("deleteByPrimaryKey Error!", exception);
                    return 0;
                }
            }
        });
    }

    @Override
    public int deleteByUserId(String userId) {
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.deleteByUserId(userId);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("deleteByUserId Error!", exception);
                    return 0;
                }
            }
        });
    }

    @Override
    public int insertOrUpdateByUserId(UserInfo userInfo) {
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    String userId = userInfo.getUserId();
                    UserInfo existUserInfo = userInfoMapper.selectByUserId(userId);
                    if (existUserInfo != null) {
                        //update
                        return userInfoMapper.updateByUserId(userInfo);
                    } else {
                        //insert
                        return userInfoMapper.insert(userInfo);
                    }
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("insertOrUpdateByUserId Error!", exception);
                    return 0;
                }
            }
        });
    }

    @Override
    public UserInfo selectByPrimaryKey(Integer id) {
        return this.transactionTemplate.execute(new TransactionCallback<UserInfo>() {
            @Override
            public UserInfo doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.selectByPrimaryKey(id);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("selectByPrimaryKey Error!", exception);
                    return null;
                }
            }
        });
    }

    @Override
    public UserInfo selectByUserId(String userId) {
        return this.transactionTemplate.execute(new TransactionCallback<UserInfo>() {
            @Override
            public UserInfo doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.selectByUserId(userId);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("selectByUserId Error!", exception);
                    return null;
                }
            }
        });
    }

    @Override
    public int updateByPrimaryKey(UserInfo userInfo) {
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.updateByPrimaryKey(userInfo);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("updateByPrimaryKey Error!", exception);
                    return 0;
                }
            }
        });
    }

    @Override
    public int updateByUserId(UserInfo userInfo) {
        return this.transactionTemplate.execute(new TransactionCallback<Integer>() {
            @Override
            public Integer doInTransaction(TransactionStatus transactionStatus) {
                try {
                    //count
                    return userInfoMapper.updateByUserId(userInfo);
                } catch (Exception exception) {
                    transactionStatus.setRollbackOnly();
                    logger.error("updateByUserId Error!", exception);
                    return 0;
                }
            }
        });
    }
}
